import { _decorator, Component, Node } from 'cc';
import { TubePartType } from './Enums';
import { Ball } from './Ball';
const { ccclass, property } = _decorator;

@ccclass('TubePart')
export class TubePart extends Component {

    @property({
        type: TubePartType,
        tooltip: '试管部件类型（底部、中间、顶部）'
    })
    tubePartType: TubePartType = TubePartType.Middle;

    @property({
        type: Ball,
        tooltip: '当前部件的球'
    })
    ball: Ball = null;

    @property(Node)
    node: Node = null; // 部件节点

    constructor(tubePartType: TubePartType) {
        super();
        this.tubePartType = tubePartType;
    }

    start() {
        
    }

    // 设置当前部件的球
    setBall(ball: Ball) {
        if (ball == null) {
        // 先清空引用和节点
        if (this.ball && this.ball.node) {
            this.ball.node.parent = null;
        }
        this.ball = null;
        return;
        }
        if (this.ball) {
            console.warn(`部件已存在球: ${this.ball.ballColor}`);
            return;
        }
        this.ball = ball;
        //ball.node.parent = this.node;
    } 

    // 获取当前部件的球
    getBall() {
        return this.ball;
    }

    // 检查当前部件是否有球
    hasBall(): boolean {
        return this.ball !== null;
    }
}


